package com.killer;

import com.killer.service.HelloService;
import com.killer.view.MonitorContext;
import com.killer.view.MonitorData;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ：Killer
 * @date ：Created in 20-12-14 上午11:33
 * @description：${description}
 * @modified By：
 * @version: version
 */
public class ConsumerMain {

    //通过线程池执行请求，以免产生过多线程
    private static ExecutorService executorService = Executors.newFixedThreadPool(200);

    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        final ClassPathXmlApplicationContext context = new
                ClassPathXmlApplicationContext("dubbo-consumer.xml");
        context.start();
        //启动一个线程每5秒统计最近一分钟的每个方法的tp
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Map<String,MonitorData> monitorDataMap= MonitorContext.getMonitorDataMap();
                    if (monitorDataMap.isEmpty()) {
                        System.out.println("暂无发现有调用");
                    }
                    for (Map.Entry<String, MonitorData> entry : monitorDataMap.entrySet()) {
                        System.out.println("方法:"+entry.getKey()+"，的TP90耗时:"+entry.getValue().statTp(0.9)+"，TP95耗时:"+entry.getValue().statTp(0.95));
                    }
                }
            }
        }).start();
        final HelloService helloService = context.getBean(HelloService.class);
        while (true) {
            System.out.println("按回车键开始发送请求:");
            System.in.read();
            for(int i=0;i<2000;i++) {
                executorService.execute(new Runnable() {
                    public void run() {
                        try {
                            helloService.methodA();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }});
                executorService.execute(new Runnable() {
                    public void run() {
                        try {
                            helloService.methodB();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }});
                executorService.execute(new Runnable() {
                    public void run() {
                        try {
                            helloService.methodC();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }});

            }
        }
    }
}
